<template>
	<view>
		<!-- 顶部导航 -->
		<fa-navbar title="商品库存" ref="navbar" ></fa-navbar>
		<view class="content">
			<u-form :model="form" ref="uForm">
				<u-form-item label="出入库门店" prop="store_name" label-width="200">
					<u-input v-model="user_name2" placeholder="请选择" @click="show2 = true"/>
				</u-form-item>
				<u-form-item label="入库数量" prop="jianum" label-width="140">
					<u-input v-model="form.jianum" :type="type" />
				</u-form-item>
				<u-form-item label="出库数量" prop="jiannum" label-width="140">
					<u-input v-model="form.jiannum" :type="type" />
				</u-form-item>
			</u-form>
			<u-button @click="submit" :custom-style="customStyle">保存</u-button>
			
			<u-form :model="form1" ref="uForm1">
				<u-form-item label="出库门店" prop="store_name" label-width="200">
					<u-input v-model="user_name" placeholder="请选择" @click="show3 = true"/>
				</u-form-item>
				<u-form-item label="入库门店" prop="store_name" label-width="200">
					<u-input v-model="user_name1" placeholder="请选择" @click="show1 = true"/>
				</u-form-item>
				<u-form-item label="调拨数量" prop="num" label-width="140">
					<u-input v-model="form1.num" :type="type" />
				</u-form-item>
				
			</u-form>
			<u-button @click="submit1" :custom-style="customStyle">调拨</u-button>
			
		</view>
		<u-toast ref="uToast" />
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
		<u-select v-model="show1" :list="list1" @confirm="confirm1"></u-select>
		<u-select v-model="show2" :list="list2" @confirm="confirm2"></u-select>
		<u-select v-model="show3" :list="list3" @confirm="confirm3"></u-select>
	</view>
</template>

<script>
	import { archives } from '@/common/fa.mixin.js';
	// import FaUploadImage from '@/pages/publish/components/fa-upload-image/fa-upload-image.vue';
	export default {
		components: {
			// FaUploadImage
		},
		mixins: [archives],
		data() {
			return {
				action: 'http://leiyu99.com/api/common/upload',
				filesArr: [],
				goods_id: 0,
				type:'number',
				show1: false,
				list1: [],
				show2: false,
				list2: [],
				show3: false,
				list3: [],
				glkc_key:0,
				customStyle: {
					marginTop: '50px', // 注意驼峰命名，并且值必须用引号包括，因为这是对象
					color: '#333',
					border: 'none',
					background: '#e0ebdd',
				},
				form: {
					id: '',
					jiannum: 0,
					jianum: 0,
				},
				user_id:'',
				user_name:'',
				user_id2:'',
				user_name2:'',
				user_id1:'',
				user_name1:'',
				form1: {
					num: 0,
					store_id:0,
				},
				rules: {
					
				}
			};
		},
		onLoad(e) {
			let query = e || {};
			this.goods_id = query.goods_id || 0;
			this.is_update = true;
			if (this.vuex_token) {
				this.getUserIndex();
				const glkc = uni.getStorageSync('glkc_key');
				if(glkc){
					this.glkc_key = glkc;
				}
				this.MemberList();
			} else {
				this.$u.vuex('vuex_user', {});
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			this.$refs.uForm1.setRules(this.rules);
		},
		methods: {
			getUserIndex() {
				let apptype = '';
				let platform = '';
				// #ifdef MP-WEIXIN
				platform = 'wechat';
				apptype = 'miniapp';
				// #endif
				this.$api.getUserIndex({ apptype, platform }).then(res => {
					uni.stopPullDownRefresh();
					if (res.code) {
						this.$u.vuex('vuex_user', res.data.userInfo);
			
						if (res.data.showProfilePrompt && !this.vuex_setting.prompted) {
							// 弹窗每次登录状态只提示一次
							this.$u.vuex('vuex_setting.prompted', true);
							uni.showModal({
								title: '温馨提示',
								confirmText: '去设置',
								cancelText: '取消',
								showCancel: true,
								content: '当前未设置昵称，请设置昵称后再继续操作',
								success: (res) => {
			
									if (res.confirm) {
										this.$u.route("/pages/my/profile");
									} else if (res.cancel) {
			
									}
								}
							});
						}
					}
				});
			},
			confirm3(e) {
				console.log(e)
				this.user_name = e[0].label;
				this.user_id = e[0].value;
			},
			confirm1(e) {
				console.log(e)
				this.user_name1 = e[0].label;
				this.user_id1 = e[0].value;
			},
			confirm2(e) {
				console.log(e)
				this.user_name2 = e[0].label;
				this.user_id2 = e[0].value;
			},
			MemberList: async function() {
				let data = {
					glkc_id:this.glkc_key
				};
				let res = await this.$api.StoreList(data);
				console.log(res,'dd')
				this.list3 = res.data;
				this.list1 = res.data;
				this.list2 = res.data;
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过',this.form);
						if(this.form.jiannum==0 && this.form.jianum==0){
							uni.showToast({
								title: '请填写数量',
								icon: 'error',
								duration: 2000
							});
							return false;
						}
						this.$api.addGoodsStock({ 
							mendian_id:this.glkc_key,
							goods_id:this.goods_id,
							user_id:this.user_id2,
							jiannum: this.form.jiannum,
							jianum: this.form.jianum,
						}).then(res => {
							console.log(res,'11')
							if(res.code == 1){
								uni.showToast({
									title: '操作成功',
									icon: 'success',
									duration: 2000,
									success: function (res1) {
										setTimeout(() => {
											uni.redirectTo({
												url: '/pages/my/user_spgl'
											});
										}, 2000);
									},
								});
			
							}else{
								uni.showToast({
									title: res.msg,
									icon: 'error',
									duration: 2000
								});
								return false;
							}
							
						});
						
					} else {
						console.log('验证失败');
					}
				});
			},
			submit1() {
				this.$refs.uForm1.validate(valid => {
					if (valid) {
						
						if(this.form1.num==0){
							uni.showToast({
								title: '请填写数量',
								icon: 'error',
								duration: 2000
							});
							return false;
						}
						this.$api.DbGoodsStock({ 
							mendian_id:this.glkc_key,
							goods_id:this.goods_id,
							user_id:this.user_id,
							user_id1:this.user_id1,
							num: this.form1.num
						}).then(res => {
							console.log(res,'11')
							if(res.code == 1){
								uni.showToast({
									title: '操作成功',
									icon: 'success',
									duration: 2000,
									success: function (res1) {
										setTimeout(() => {
											uni.redirectTo({
												url: '/pages/my/user_spgl'
											});
										}, 2000);
									},
								});

							}else{
								uni.showToast({
									title: res.msg,
									icon: 'error',
									duration: 2000
								});
								return false;
							}
							
						});
						
					} else {
						console.log('验证失败');
					}
				});
			},

			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				var str = e.toString();
				this.form.goods_ids = str;
			},
			 // 上传成功回调
			handleSuccess(res, index, lists) {
				uni.showToast({
					title: '上传成功',
					icon: 'success'
				});
				// 更新文件列表
				this.form.fileList = this.form.fileList.concat(res.data.url);
				
				console.log(this.form.fileList,'concat')
			},
			// 文件被移除
			handleRemove(index, lists) {
				console.log(index,'index')
				this.form.fileList.splice(index, 1);
				console.log(this.form.fileList,'fileList')
			},
			confirm(e) {
				this.form.name = e[0].label;
				this.form.id = e[0].value;

			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.content{
		width: 90%;margin: 10px auto;
	}
</style>
